package threadpoolnio.client;

import java.io.*;
import java.net.Socket;

/**
 * @author ：xx
 * @date ：Created in 2020/3/12 16:32
 * @description：
 * @modified By：
 */
public class Client {
    private Socket socket=null;
    private PrintWriter outMsg=null;
    private FileOutputStream outFile=null;
    private InputStream in=null;
    private String ip=null;
    private int port;

    public Client(String ip, int port){
        try {
            this.ip=ip;
            this.port=port;
            socket=new Socket(ip,port);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void sendMsg(String msg){
        try {
            if(msg!=null&&msg.length()>0) {
                if(outMsg==null)
                    outMsg =  new PrintWriter(new OutputStreamWriter(socket.getOutputStream(),"GBK"));
                outMsg.println(msg);
                //刷新流里面的数据
                outMsg.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public void close(){
        try {
            if(outMsg!=null)
                outMsg.close();
            if(outFile!=null)
                outFile.close();
            if(in!=null)
                in.close();
            if(socket!=null)
                socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
